﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Db;

namespace Aandelendelen.Admin
{
	public partial class AdminShowMails : AdminBaseForm
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			this.Table1.Attributes["border"] = "1";

			if (!this.IsPostBack)
			{
				DateTime today = DateTime.Today;
				DateTime tomorrow = today.AddDays(1.0);

				this.FromDate.NumberOfYears = 2;
				this.ToDate.NumberOfYears = 2;

				this.FromDate.SelectedDate = today;
				this.ToDate.SelectedDate = tomorrow;
			}

			this.ShowMails();
		}

		private void ShowMails()
		{
			QueryArguments args = new QueryArguments();
			args.Add("datefrom", this.FromDate.SelectedDate, System.Data.OleDb.OleDbType.Date);
			args.Add("dateto", this.ToDate.SelectedDate, System.Data.OleDb.OleDbType.Date);

			Query query = new Query("s_getmails", args);

			ResultSet resultSet = (ResultSet)query.Execute();

			var records = this.GetUniqueText(resultSet);

			foreach (UniqueText uniqueText in records.Values)
			{
				this.AddRow(uniqueText);
			}
		}

		private void AddRow(UniqueText uniqueText)
		{
			TableRow row = new TableRow();

			TableCell enteredCell = new TableCell();
			enteredCell.VerticalAlign = VerticalAlign.Top;
			enteredCell.Text = uniqueText.DateEntered.ToString("dd-MM-yyyy, HH:mm:ss");

			TableCell recipientsCell = new TableCell();
			recipientsCell.VerticalAlign = VerticalAlign.Top;

			if (uniqueText.Count == 1)
				recipientsCell.Text = uniqueText[0];
			else
			{
				DropDownList lb = new DropDownList();
				foreach (string str in uniqueText)
				{
					lb.Items.Add(str);
				}
				recipientsCell.Controls.Add(lb);
			}

			TableCell subjectCell = new TableCell();
			subjectCell.Text = uniqueText.Subject;
			subjectCell.VerticalAlign = VerticalAlign.Top;

			TableCell bodyCell = new TableCell();
			bodyCell.Text = uniqueText.Body;
			bodyCell.VerticalAlign = VerticalAlign.Top;

			row.Cells.Add(enteredCell);
			row.Cells.Add(recipientsCell);
			row.Cells.Add(subjectCell);
			row.Cells.Add(bodyCell);

			this.Table1.Rows.Add(row);
		}

		private Dictionary<int, UniqueText> GetUniqueText(ResultSet resultSet)
		{
			var records = new Dictionary<int, UniqueText>(resultSet.Count);

			for (int i = 0; i < resultSet.Count; i++)
			{
				int textId = Convert.ToInt32(resultSet[i, "textid"]);

				UniqueText unique = null;

				if (records.TryGetValue(textId, out unique) == false)
				{
					unique = new UniqueText((string)resultSet[i, "body"], (string)resultSet[i, "subject"], (DateTime)resultSet[i, "entered"]);

					records[textId] = unique;
				}
				unique.Add((string)resultSet[i, "recipient"]);
			}

			return records;
		}

		private class UniqueText : StringCollection
		{
			private string body;
			private string subject;
			private DateTime dateEntered;

			internal UniqueText(string body, string subject, DateTime dateEntered)
			{
				this.body = body;
				this.subject = subject;
				this.dateEntered = dateEntered;
			}

			internal string Body
			{
				get { return this.body; }
			}

			internal string Subject
			{
				get { return this.subject; }
			}

			internal DateTime DateEntered
			{
				get { return this.dateEntered; }
			}
		}
	}
}